package cn.yuan.pet.controller;

import cn.yuan.basic.Exception.BusinessException;
import cn.yuan.basic.util.LoginContext;
import cn.yuan.basic.util.PageList;
import cn.yuan.pet.domain.Pet;
import cn.yuan.pet.domain.PetDetail;
import cn.yuan.pet.query.PetQuery;
import cn.yuan.pet.service.IPetService;
import cn.yuan.user.domain.Logininfo;
import cn.yuan.util.AjaxResult.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/pet")
@Api(tags = "宠物接口",description = "宠物接口详细描述")
public class PetController {
    @Autowired
    private IPetService petService;
    /**
     * 查询一个  localhost:8080/pet/findone  1
     * localhost:8080/pet/1
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "宠物查找",notes = "根据ID查找宠物")
    public Pet findOne(@PathVariable("id") Long id){
        return petService.loadById(id);
    }

    /**
     * 查询一个  localhost:8080/pet/findone  1
     * localhost:8080/pet/1
     * @return
     */
    @GetMapping("/detail/{id}")
    @ApiOperation(value = "宠物查找",notes = "根据ID查找宠物")
    public PetDetail findDetailByPetId(@PathVariable("id") Long id){
        return petService.findDetailByPetId(id);
    }

    /**
     * 查询所有localhost:8080/pet
     * @return
     */
    @GetMapping
    @ApiOperation(value = "宠物查找",notes = "查找所有宠物")
    public List<Pet> findAll(){
        List<Pet> list = petService.loadAll();
        return list;
    }

    @PutMapping  /*@RequestBody  接收请求体localhost:8080/pet*/
    @ApiOperation(value = "添加或者修改宠物",notes = "添加或者修改宠物,包含详情")
    public AjaxResult addorUpdate(@RequestBody Pet pet){
        try {
            if (pet.getId()==null){//添加
                petService.save(pet);
            }else{//修改
                petService.update(pet);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("操作失败");
        }
    }

    @DeleteMapping("/{id}")  /*@RequestBody  接收请求体localhost:8080/pet/4*/
    @ApiOperation(value = "删除宠物",notes = "根据ID删除宠物和详情")
    public AjaxResult delete(@PathVariable("id")Long id){
        try {
            petService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("操作失败");
        }
    }

    @PostMapping
    @ApiOperation(value = "分页查找宠物",notes = "分页查找宠物")/*localhost:8080/pet*/
    public PageList<Pet> queryForPage(@RequestBody PetQuery petQuery){
        PageList<Pet> pageList = petService.queryForPage(petQuery);
        return pageList;
    }

    @PostMapping("/list")
    @ApiOperation(value = "前端主站分页查找宠物",notes = "前端主站分页查找宠物")
    public PageList<Pet> queryList(@RequestBody PetQuery petQuery){
        petQuery.setState(1);//只查询上架的
        PageList<Pet> pageList = petService.queryForPage(petQuery);
        return pageList;
    }

    @PostMapping("/onsale")
    public AjaxResult onSale(@RequestBody List<Long> ids){
        try {
            petService.onOrOffSale(ids,1);// 0 下架  1 上架
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setMsg(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("系统错误");
        }
    }

    @PostMapping("/offsale")
    public AjaxResult offSale(@RequestBody List<Long> ids){
        try {
            petService.onOrOffSale(ids,0);// 0 下架  1 上架
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setMsg(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("系统错误");
        }
    }

    /**
     * 查询一个  localhost:8080/pet/findone  1
     * localhost:8080/pet/1
     * @return
     */
    @GetMapping("/info/{id}")
    @ApiOperation(value = "详细信息",notes = "根据ID查找宠物详情")
    public Pet info(@PathVariable("id") Long id){
        return petService.loadByIdDetail(id);
    }

    /**
     * 领养宠物
     * @return
     */
    @GetMapping("/adopt/{id}")
    @ApiOperation(value = "领养宠物",notes = "根据ID领养宠物")
    public AjaxResult adopt(@PathVariable("id") Long id, HttpServletRequest request){
        try {
            Logininfo loginInfo = LoginContext.getLoginInfo(request);
            petService.adopt(id,loginInfo.getId());
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("领养失败！"+e.getMessage());
        }
    }
}
